from frame_work.setting import *
from frame_work.yaml_file import YAML
from frame_work.logger import Logger
from frame_work.login import TfxAdminLogin, AdminLogin
from frame_work import menu_list
from frame_work.util import assert_result_dict, get_sql_info

logger = Logger(__name__).get_logger()


# 会员的操作
class User(object):

    def __init__(self, name):
        self.uname = name
        self.session = self.get_session()
        self.uid = self.get_uid()
        self.yaml = YAML(os.path.join(base_Dir, 'data', 'data.yaml'))

    def get_uid(self):
        """
        查询用户uid
        :return:
        """
        statement = """select uid from sk_user where uname = %s"""
        params = (self.uname,)
        value = get_sql_info(statement, params, meta={
            "info": "查询uid"
        })
        return value[0].get("uid")

    def get_session(self):
        # 管理员登录提取session
        login = AdminLogin()
        login.admin_card_login()
        return login.get_session()

    def block_user(self):
        """
        对用户进行屏蔽操作
        :return:
        """
        yaml_data = self.yaml.get_data('block_user')
        url = yaml_data.get('url') + '/?uids=' + str(self.uid)
        logger.debug("开始屏蔽用户")
        response = self.session.post(url, data=yaml_data.get(
            'data'), headers=yaml_data.get('header')).text
        return assert_result_dict(response, '屏蔽用户' + self.uname)

    def unblock_user(self):
        """
        对用户进行解除屏蔽操作
        :return:
        """
        yaml_data = self.yaml.get_data('unblock_user')
        url = yaml_data.get('url') + '/?uids=' + str(self.uid)
        logger.debug("开始解除屏蔽用户")
        response = self.session.get(url, headers=yaml_data.get('header')).text
        return assert_result_dict(response, '解除屏蔽用户' + self.uname)

    def ban_user(self):
        """
        对用户进行封号操作
        :return:
        """
        yaml_data = self.yaml.get_data('ban_user')
        url = yaml_data.get('url') + '/?uids=' + str(self.uid)
        logger.debug("开始封号用户")
        response = self.session.post(url, data=yaml_data.get(
            'data'), headers=yaml_data.get('header')).text
        status = assert_result_dict(response, '用户封号' + self.uname)
        return status

    def unban_user(self):
        """
        对用户进行解除封号操作
        :return:
        """
        yaml_data = self.yaml.get_data('unban_user')
        url = yaml_data.get('url') + '/?uids=' + str(self.uid)
        logger.debug("开始解除封号用户")
        response = self.session.get(url, headers=yaml_data.get('header')).text
        status = assert_result_dict(response, '解除封号' + self.uname)
        return status

    def clear_email_mobile(self):
        """
        清空用户的手机邮箱
        :return:
        """
        yaml_data = self.yaml.get_data('clear_email_mobile')
        yaml_data.get('data').update({
            'uid': self.uid
        })
        logger.debug("开始清空用户的手机邮箱")
        response = self.session.post(
            url=yaml_data.get('url'),
            data=yaml_data.get('data'),
            headers=yaml_data.get('header')).text
        status = assert_result_dict(response, '清空用户手机邮箱' + self.uname)
        return status


def main():
    while True:
        # 显示操作用户功能菜单
        menu_list.user_menu()
        # 选择
        choise = input("请输入您的选择: ")
        if choise in ('q', 'Q'):
            break
        uname = input(u'请输入用户名称: ')
        user = User(uname)
        if choise == '1':
            user.block_user()
        elif choise == '2':
            user.unblock_user()
        elif choise == '3':
            user.ban_user()
        elif choise == '4':
            user.unban_user()
        elif choise == '5':
            user.clear_email_mobile()
        else:
            logger.info("没有对应的选项,请按选项输入...")
            break


if __name__ == "__main__":
    main()
